/**
 * 
 */
package vn.lumi.smarthome.network;

import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.io.model.Scene;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * @author Chu Tuan Anh
 *
 */
public class UpdateSceneActivated extends HCMsg{

	//constant
	public static final int LENGTH = 3;
	
	//variables
	private int userId = 0;
	private int sceneId = 0;

	private boolean isCorrectFormat = false;
	
	//byte positions
	private static final int userIdPos = 1;
	
	/**
	 * @param cmd_id
	 * @param Msg_id
	 * @param len
	 */
	public UpdateSceneActivated(int userId) {
		super(NetMessage.CMD_HC_UPDATE_SCENE_ACTIVATED, 0, LENGTH);
		// TODO Auto-generated constructor stub
		this.userId = userId;
	}


	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.HCMsg#parseMessage(byte[], int)
	 */
	@Override
	public void parseMessage(byte[] input, int length) {
		// TODO Auto-generated method stub
		isCorrectFormat = isCorrectFormat(input,length);
		if(isCorrectFormat == false) return;
		
		this.sceneId = Byte2Unsigned(input[2]);
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.HCMsg#doWhenReceived(android.content.Context)
	 */
	@Override
	public void doWhenReceived(Context context) {
		// TODO Auto-generated method stub
		if(isCorrectFormat == false) return;
		//find room of scene
		int roomID = 0;
		Uri uri=TBL_Scene.CONTENT_URI_SCENE;
        String[] projection=new String[]{TBL_Scene.ROOM_ID};
        String selection=TBL_Device._ID+"=?";
        String[] selectionArgs=new String[]{this.sceneId+""};
        Cursor cursor=context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if(cursor!=null){
            if(cursor.moveToFirst()) 
                    roomID=cursor.getInt(cursor.getColumnIndex(TBL_Scene.ROOM_ID));
        }
        else return;
        
        Log.d("ActivateScene","room_id="+roomID);
        
        //find all scenes of this room
        projection = new String[]{TBL_Scene._ID, TBL_Scene.STATE};
        selection = TBL_Scene.ROOM_ID+"=?";
        selectionArgs = new String[]{roomID+""};
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if(cursor==null) return;
        cursor.moveToFirst();
        while(!cursor.isAfterLast()){
        	int sceneID = cursor.getInt(cursor.getColumnIndex(TBL_Scene._ID));
        	int sceneState = cursor.getInt(cursor.getColumnIndex(TBL_Scene.STATE));
        	if(sceneID != this.sceneId && sceneState == Scene.SCENE_STATE_ON){
        		//set this scene to off
        		ContentValues values = new ContentValues();
        		values.put(TBL_Scene.STATE, Scene.SCENE_STATE_OFF);
        		selection = TBL_Scene._ID+"=?";
        		selectionArgs = new String[]{sceneID+""};
        		context.getContentResolver().update(uri, values, selection, selectionArgs);
        		Log.d("ActivateScene","turn off sceneID=" + sceneID);
        	}
        	else if(sceneID == this.sceneId){
        		//set this scene to on
        		ContentValues values = new ContentValues();
        		values.put(TBL_Scene.STATE, Scene.SCENE_STATE_ON);
        		selection = TBL_Scene._ID+"=?";
        		selectionArgs = new String[]{sceneID+""};
        		context.getContentResolver().update(uri, values, selection, selectionArgs);
        		Log.d("ActivateScene","turn on sceneID=" + sceneID);
        		
        	}
        	cursor.moveToNext();
        }
	}
	
	@Override
	public boolean isCorrectFormat(byte[] input, int length){
		boolean result = false;
		if(super.isCorrectFormat(input, length) == false)
			result =  false;
		else{
			if(this.userId == Byte2Unsigned(input[userIdPos])) result =  true;
			else result =  false;
		}
		return result;
	}

}
